<?php

/**
 * @version February 6, 2011
 * @Author Brice
 * This is the view file.
 * It controls the category file to manipulate data
 *
 */
echo "<html><body>";

include '../db-control/inc/functions.php';
require '../model/Category.php';
include '../db-control/dbCategory.php';

//update a category if the useZr is trying to do so
if ($_POST['update'] != '') {
    $name = $_POST['update'];
    $c = new Category(); //creates a new instance of Category()
    $c->setCategoryID($categoryID); //sets the category id
    $db = new dbCategory();
    $exists = $db->checkID($c); //checks to see if the name is a real category
    if (!$exists) {
        echo "<b>Category with name '$name' does not exist...</b>";
    } else {
        $c->setID($_POST[categoryID]); //sets the category id
        $result = $db->updateID($c); //finally, updates the category with the data we've set
        if (!$result) {
            echo "<b>Could not update category with name '$name'...</b>";
        }
    }
} else {
    if (isset($_POST['add'])) {
        $c = new Category();
        $c->setID($_POST['categoryID']);
        $db = new dbCategory();
        $result = $db->addCategory($c);
        if (!$result) {
            echo "<b>Please enter some category information</b>";
        }
    }

    if (isset($_POST['deleteButton'])) {
        $c = new Category(); //creates a new category object (i.e. creates a new category)
        $c->setCategoryID($_POST['deleteID']);
        $db = new dbCategory();
        $result = $db->deleteCategory($c);
        if (!$result) {
            echo "<b>Could not delete category with name '$_POST[deleteID]'...</b>";
        }
    }
}


//displays the web form used for adding a new category
echo <<<_FORM


<form action='viewCategory.php' method='post'>
<table>
    <tr><td colspan='2'>Specify an name to change a category's data</td><td><input type='text' size='4' name='update' /></td></tr>
    <tr><td>Category ID:</td><td><input type='int' value='' name='name' /></td></tr>
           <tr><td>Name:</td><td><input type='text' value='' name='name' /></td></tr>
    <tr><td></td><td align='right'><input type='submit' value='Add Category' name='add'/></td></tr>
</table>
</form>


_FORM;

displayCategories(); //displays the category list


echo "</body></html>";

function displayCategories() {
    $db = new dbCategory(); //gets all the categories from the database
    $result = $db->getAllCategories();
    echo "<table width='200px'>";
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        echo "<tr><td>$row[id]</td><td></td></tr>";
        echo "<tr><td align='right'>Category ID:</td><td align='left'>$row[categoryID]</td></tr>";
        echo "<tr><td align='right'>Name:</td><td align='left'>$row[name]</td></tr>";
        echo "<tr><td colspan='2' align='right'><form action='viewCategory.php' method='post'><input type='hidden' value='$row[categoryID]' name='deleteID' /><input type='submit' value='Delete' name='deleteButton' /></form></td><td></td></tr>";
        echo "<tr><td colspan='2'><hr /></td><td></td></tr>";
    }
    echo "</table>";
}
?>